package cn.wolfcode.luowowo.comment.service.impl;

import cn.wolfcode.luowowo.comment.domain.StrategyComment;
import cn.wolfcode.luowowo.comment.query.StrategyCommentQueryObject;
import cn.wolfcode.luowowo.comment.repository.IStrategyRepository;
import cn.wolfcode.luowowo.comment.service.IStrategyCommentService;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
public class StrategyCommentServiceImpl implements IStrategyCommentService {
    @Autowired
    private MongoTemplate mongoTemplate; //处理比较复杂的操作
    @Autowired
    private IStrategyRepository mongeRepository; //处理简单的crud

    @Override
    public void saveOrUpdate(StrategyComment strategyComment) {
        //createTime
        if(strategyComment.getId() == null){
            strategyComment.setCreateTime(new Date());
        }
        /*
            //编辑操作
            //thumbupnum
            //thumbuplist
        */
        mongeRepository.save(strategyComment);
    }

    @Override
    public Page<StrategyComment> query(StrategyCommentQueryObject qo) {
        //查询封装对象
        Query query = new Query();
        //查询条件
        Criteria criteria = null;
        if(qo.getDetailId() != -1){
            criteria = Criteria.where("detailId").is(qo.getDetailId());
        }else {
            criteria = Criteria.where("detailId").ne(qo.getDetailId());
        }
        //添加查询条件
        query.addCriteria(criteria);
        //查询条数
        long count = mongoTemplate.count(query, StrategyComment.class);
        //优化
        if(count == 0){
            return Page.empty();
        }

        //添加排序条件
        query.with(new Sort(Sort.Direction.DESC, "createTime"));
        //添加分页条件
        Pageable pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize());
        query.with(pageable);

        //查询评论数据
        List<StrategyComment> list = mongoTemplate.find(query, StrategyComment.class);
        return new PageImpl<>(list, pageable, count);
    }

    @Override
    public boolean thumbUp(String toid, Long fromid) {
        Optional<StrategyComment> optional = mongeRepository.findById(toid);
        boolean flag = false;
        if(optional.isPresent()){ //对象中有值时
            StrategyComment strategyComment = optional.get();
            List<Long> list = strategyComment.getThumbuplist();
            if(list.contains(fromid)){ //取消点赞
                list.remove(fromid);
                strategyComment.setThumbupnum(strategyComment.getThumbupnum() - 1);
            }else{ //点赞
                list.add(fromid);
                strategyComment.setThumbupnum(strategyComment.getThumbupnum() + 1);
                flag = true;
            }
            saveOrUpdate(strategyComment); //更新数据
        }
        return flag;
    }
}
